﻿using System;
using System.Collections.Generic;
using ArchPostprocessing.CompoundStructureManage.DataDefinition;
using Autodesk.Revit.DB;
using YArchitech.Revit;
using YJKArch.Utils;
using TopologySearch;

namespace ArchPostprocessing.CompoundStructureManage.Managers
{
	public class ColumnCommon
	{
		public static void GetColumns(Autodesk.Revit.DB.Document doc, ref List<FamilyInstance> selColumns)
		{
			foreach (Element element in new FilteredElementCollector(doc, doc.ActiveView.Id).OfCategory(BuiltInCategory.OST_Columns).OfClass(typeof(FamilyInstance)).ToElements())
			{
				FamilyInstance item = element as FamilyInstance;
				selColumns.Add(item);
			}
			foreach (Element element2 in new FilteredElementCollector(doc, doc.ActiveView.Id).OfCategory(BuiltInCategory.OST_StructuralColumns).OfClass(typeof(FamilyInstance)).ToElements())
			{
				FamilyInstance item2 = element2 as FamilyInstance;
				selColumns.Add(item2);
			}
		}

		public static bool GetColumnElevationInfo(FamilyInstance column, Autodesk.Revit.DB.Document doc, ref double columnElevation, ref double columnHeight)
		{
			bool result;
			try
			{
				ElementId elementId = column.get_Parameter(BuiltInParameter.FAMILY_BASE_LEVEL_PARAM).AsElementId();
				double elevation = (doc.GetElement(elementId) as Level).Elevation;
				double num = column.get_Parameter(BuiltInParameter.FAMILY_BASE_LEVEL_OFFSET_PARAM).AsDouble();
				columnElevation = elevation + num;
				ElementId elementId2 = column.get_Parameter(BuiltInParameter.FAMILY_TOP_LEVEL_PARAM).AsElementId();
				double elevation2 = (doc.GetElement(elementId2) as Level).Elevation;
				double num2 = column.get_Parameter(BuiltInParameter.FAMILY_TOP_LEVEL_OFFSET_PARAM).AsDouble();
				double num3 = elevation2 + num2;
				columnHeight = num3 - columnElevation;
				result = true;
			}
			catch (Exception)
			{
				result = false;
			}
			return result;
		}

		public static List<Pole2D> GetSolidProfile(Solid solid, ElementId columnId, double offset, ref List<ProfileInfo> columnProfileLoopList)
		{
			List<Pole2D> list = new List<Pole2D>();
			foreach (object obj in solid.Faces)
			{
				Face face = (Face)obj;
				if (!(face.GetType() != typeof(PlanarFace)) && Geometry.IsEqual((face as PlanarFace).FaceNormal(), -XYZ.BasisZ))
				{
					foreach (object obj2 in face.EdgeLoops)
					{
						EdgeArray edgeArray = (EdgeArray)obj2;
						List<XYZ> list2 = new List<XYZ>();
						new List<EdgeInfo>();
						foreach (object obj3 in edgeArray)
						{
							Curve curve = ((Edge)obj3).AsCurve();
							Pole2D item = Pole2D.TransformWallToStructure2DPole(curve, columnId, offset, offset);
							list.Add(item);
							IList<XYZ> list3 = curve.Tessellate();
							for (int i = 0; i < list3.Count - 1; i++)
							{
								list2.Add(list3[i]);
							}
						}
						ProfileInfo item2 = new ProfileInfo(list2);
						columnProfileLoopList.Add(item2);
					}
				}
			}
			return list;
		}
	}
}
